using Microsoft.AspNetCore.Mvc;
using zhwy.saas.api.Models;
using zhwy.saas.api.Services;

namespace zhwy.saas.api.Controllers
{
    /// <summary>
    /// 基础控制器
    /// </summary>
    /// <typeparam name="TEntity">实体类型</typeparam>
    [ApiController]
    [Route("api/v{version:apiVersion}/[controller]")]
    public abstract class BaseController<TEntity> : ControllerBase where TEntity : class, new()
    {
        protected readonly IBaseService<TEntity> _service;

        protected BaseController(IBaseService<TEntity> service)
        {
            _service = service;
        }

        protected ApiResponseModel<T> GetResponseModel<T>(T data)
        {
            return new ApiResponseModel<T>
            {
                Code = 200,
                Message = "成功",
                Data = data,
                Success = true
            };
        }

        protected ApiResponseModel<T> GetResponseErrorModel<T>(T data, int code = 500, string msg = "")
        {
            return new ApiResponseModel<T>
            {
                Code = code,
                Message = "失败",
                Data = default,
                Success = false
            };
        }

        ///// <summary>
        ///// 获取指定ID的实体
        ///// </summary>
        ///// <param name="id">实体ID</param>
        ///// <returns>实体信息</returns>
        ///// <response code="200">成功获取实体</response>
        ///// <response code="404">实体不存在</response>
        //[HttpGet("{id}")]
        //public virtual async Task<ActionResult<ApiResponse<TEntity>>> Get(object id)
        //{
        //    var entity = await _service.GetByIdAsync(id);
        //    return entity == null
        //        ? ApiResponse<TEntity>.Error("未找到记录", 404)
        //        : ApiResponse<TEntity>.Ok(entity);
        //}

        ///// <summary>
        ///// 获取所有实体列表
        ///// </summary>
        ///// <returns>实体列表</returns>
        ///// <response code="200">成功获取列表</response>
        //[HttpGet]
        //public virtual async Task<ActionResult<ApiResponse<List<TEntity>>>> GetList()
        //{
        //    var list = await _service.GetAllAsync();
        //    return ApiResponse<List<TEntity>>.Ok(list);
        //}

        ///// <summary>
        ///// 分页获取实体列表
        ///// </summary>
        ///// <param name="pageIndex">页码，从1开始</param>
        ///// <param name="pageSize">每页记录数</param>
        ///// <returns>分页数据</returns>
        ///// <response code="200">成功获取分页数据</response>
        //[HttpGet("page")]
        //public virtual async Task<ActionResult<ApiResponse<PagedList<TEntity>>>> GetPage(
        //    [FromQuery] int pageIndex = 1,
        //    [FromQuery] int pageSize = 10)
        //{
        //    var result = await _service.GetPagedListAsync(x => true, pageIndex, pageSize);
        //    return ApiResponse<PagedList<TEntity>>.Ok(new PagedList<TEntity>
        //    {
        //        Items = result.Items,
        //        Total = result.Total,
        //        PageIndex = pageIndex,
        //        PageSize = pageSize
        //    });
        //}

        ///// <summary>
        ///// 添加新实体
        ///// </summary>
        ///// <param name="entity">实体信息</param>
        ///// <returns>添加结果</returns>
        ///// <response code="200">添加成功</response>
        ///// <response code="400">添加失败</response>
        //[HttpPost]
        //[ProducesResponseType(typeof(ApiResponse<bool>), 200)]
        //[ProducesResponseType(typeof(ApiResponse<bool>), 400)]
        //public virtual async Task<ActionResult<ApiResponse<bool>>> Add([FromBody] TEntity entity)
        //{
        //    var result = await _service.AddAsync(entity);
        //    return result
        //        ? ApiResponse<bool>.Ok(true, "添加成功")
        //        : ApiResponse<bool>.Error("添加失败");
        //}

        ///// <summary>
        ///// 更新实体
        ///// </summary>
        ///// <param name="entity">实体信息</param>
        ///// <returns>更新结果</returns>
        ///// <response code="200">更新成功</response>
        ///// <response code="400">更新失败</response>
        //[HttpPut]
        //[ProducesResponseType(typeof(ApiResponse<bool>), 200)]
        //[ProducesResponseType(typeof(ApiResponse<bool>), 400)]
        //public virtual async Task<ActionResult<ApiResponse<bool>>> Update([FromBody] TEntity entity)
        //{
        //    var result = await _service.UpdateAsync(entity);
        //    return result
        //        ? ApiResponse<bool>.Ok(true, "更新成功")
        //        : ApiResponse<bool>.Error("更新失败");
        //}

        ///// <summary>
        ///// 删除实体
        ///// </summary>
        ///// <param name="id">实体ID</param>
        ///// <returns>删除结果</returns>
        ///// <response code="200">删除成功</response>
        ///// <response code="400">删除失败</response>
        //[HttpDelete("{id}")]
        //[ProducesResponseType(typeof(ApiResponse<bool>), 200)]
        //[ProducesResponseType(typeof(ApiResponse<bool>), 400)]
        //public virtual async Task<ActionResult<ApiResponse<bool>>> Delete(object id)
        //{
        //    var result = await _service.DeleteAsync(id);
        //    return result
        //        ? ApiResponse<bool>.Ok(true, "删除成功")
        //        : ApiResponse<bool>.Error("删除失败");
        //}
    }
}